package com.resume.common.exception;

import com.resume.model.result.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 处理参数验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleValidationException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getFieldErrors()
                .stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.joining(", "));
        return Result.error(400, "处理参数验证异常: " + message);
    }
    
    /**
     * 处理绑定异常
     */
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        String message = e.getBindingResult().getFieldErrors()
                .stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.joining(", "));
        return Result.error(400, "处理绑定异常: " + message);
    }
    
    /**
     * 处理运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e) {
        String message = e.getMessage();
        
        // 如果消息为空，打印堆栈并使用异常类名
        if (message == null || message.trim().isEmpty()) {
            e.printStackTrace();
            return Result.error(500, "服务器内部错误: " + e.getClass().getSimpleName());
        }
        
        // 解析错误码和错误信息
        if (message.contains(":")) {
            String[] parts = message.split(":", 2);
            try {
                int code = Integer.parseInt(parts[0]);
                String msg = parts[1];
                return Result.error(code, msg);
            } catch (NumberFormatException ex) {
                // 如果解析失败，返回原始消息
                return Result.error(500, message);
            }
        }
        
        // 默认错误
        return Result.error(500, "服务器内部错误: " + message);
    }
    
    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        String message = e.getMessage();
        if (message == null || message.trim().isEmpty()) {
            return Result.error(500, "服务器内部错误: " + e.getClass().getSimpleName());
        }
        return Result.error(500, "服务器内部错误: " + message);
    }
}
